package com.begye.smartloan.apptest.disclosure.dialog

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.databinding.DiaDisclosureReadBinding

class DisclosureReadDialogFragment(private val clickAction: () -> Unit ) : DialogFragment() {

    private var _binding: DiaDisclosureReadBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val layout = inflater.inflate(R.layout.dia_disclosure_read, container, false)
        _binding = DiaDisclosureReadBinding.bind(layout)
        return layout
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Theme_Bottom_Dialog)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)

        binding.diaDisclosureCancel.setOnClickListener {
            dismiss()
        }

        binding.diaDisclosureClose.setOnClickListener {
            dismiss()
        }

        binding.diaDisclosureOk.setOnClickListener {
            clickAction.invoke()
            dismiss()
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onStart() {
        super.onStart()
        val dialog = dialog ?: return
        val window = dialog.window ?: return

        window.attributes.gravity = Gravity.CENTER
        window.setLayout(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
    }
}